package cn.hchaojie.daily.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class TestMysql {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		// 使用jdbc   java database connection
		
		// 加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		
		// 创建连接
		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/daily", "root", "root");
		
		// 发送sql请求、得到一个结果集
		PreparedStatement statement = connection.prepareStatement(
				"select id, content, created, uid, digest, views from daily");
		ResultSet resultSet = statement.executeQuery();
		
		// 遍历结果集
		while (resultSet.next()) {
//			long id = resultSet.getLong(0);
//			String content = resultSet.getString(1);
//			
//			// java.util.Date => 年月日，时分秒
//			// java.sql.Date => 年月日
//			// Date date = resultSet.getDate(2);
//			Date date = resultSet.getTimestamp(2);
//			
//			long uid = resultSet.getLong(3);
//			String digest = resultSet.getString(4);
//			int views = resultSet.getInt(5);
			
			long id = resultSet.getLong("id");
			String content = resultSet.getString("content");
			
			// java.util.Date => 年月日，时分秒
			// java.sql.Date => 年月日
			// Date date = resultSet.getDate();
			Date date = resultSet.getTimestamp("created");
			
			long uid = resultSet.getLong("uid");
			String digest = resultSet.getString("digest");
			int views = resultSet.getInt("views");

			
			String result = String.format("id:%d, content:%s, date:%tF, uid:%d, digest:%s, views:%d", 
					id, content, date, uid, digest, views);
			
			System.out.println(result);
		}
		
		resultSet.close();
		statement.close();
		connection.close();
	}
}
